package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.contentcollection.ContentCollectionAddRequest;
import com.yupi.yuoj.model.dto.contentcollection.ContentCollectionQueryRequest;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.ContentVO;
import com.yupi.yuoj.service.ContentCollectionService;
import com.yupi.yuoj.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内容收藏接口
 *
 * @author jingjin
 */
@RestController
@RequestMapping("/content_collection")
@Slf4j
public class ContentCollectionController {

    @Resource
    private ContentCollectionService contentCollectionService;

    @Resource
    private UserService userService;

    /**
     * 收藏 / 取消收藏
     *
     * @param contentCollectionAddRequest
     * @param request
     * @return resultNum 收藏变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doContentCollection(@RequestBody ContentCollectionAddRequest contentCollectionAddRequest,
            HttpServletRequest request) {
        if (contentCollectionAddRequest == null || contentCollectionAddRequest.getContentId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能收藏
        final User loginUser = userService.getLoginUser(request);
        long contentId = contentCollectionAddRequest.getContentId();
        int result = contentCollectionService.doContentCollection(contentId, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 获取我收藏的内容列表
     *
     * @param contentCollectionQueryRequest
     * @param request
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<ContentVO>> listMyContentCollectionByPage(@RequestBody ContentCollectionQueryRequest contentCollectionQueryRequest,
            HttpServletRequest request) {
        if (contentCollectionQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long current = contentCollectionQueryRequest.getCurrent();
        long size = contentCollectionQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<ContentVO> contentVOPage = contentCollectionService.listContentCollectionByPage(
                new Page<>(current, size),
                contentCollectionQueryRequest.getContentQueryRequest(), loginUser.getId());
        return ResultUtils.success(contentVOPage);
    }

}